﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace SSM.DeepZoomViewerMonitor
{
    public partial class DeepZoomViewerMonitor : UserControl
    {
        private Bitmap bufferedImage;

        /// <summary>
        /// 화면에 출력할 이미지
        /// </summary>
        private Bitmap image;
        public Bitmap Image
        {
            set
            {
                image = value;

                Invalidate();
            }
        }

        public int monitorWidth;
        public int MonitorWidth
        {
            get
            {
                return monitorWidth;
            }

            set
            {
                monitorWidth = value;

                Invalidate();
            }
        }

        public int monitorHeight;
        public int MonitorHeight
        {
            get
            {
                return monitorHeight;
            }

            set
            {
                monitorHeight = value;

                Invalidate();
            }
        }

        public int viewportX;
        public int ViewportX
        {
            get
            {
                return viewportX;
            }

            set
            {
                viewportX = value;

                Invalidate();
            }
        }

        public int viewportY;
        public int ViewportY
        {
            get
            {
                return viewportY;
            }

            set
            {
                viewportY = value;

                Invalidate();
            }
        }

        /// <summary>
        /// 생성자
        /// </summary>
        public DeepZoomViewerMonitor()
        {
            InitializeComponent();

            Resize += new EventHandler(DeepZoomMonitor_Resize);
        }

        /// <summary>
        /// 화면이 리사이즈 될때 PictureBox의 사이즈를 조절한다.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeepZoomMonitor_Resize(object sender, EventArgs e)
        {
            if (bufferedImage != null)
            {
                bufferedImage.Dispose();
                bufferedImage = null;
            }

            bufferedImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);

            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics graphics = Graphics.FromImage(bufferedImage);

            graphics.Clear(Color.White);

            // 아직 로드 되지 않았으면 화면을 그리지 않는다.
            if (image == null)
            {
                e.Graphics.DrawImage(bufferedImage, 0, 0);

                return;
            }

            Rectangle srcRectangle = new Rectangle(
                0, 0, image.Width, image.Height
                );

            Rectangle dstRectangle = new Rectangle(
                0, 0, Width, Height
                );

            graphics.DrawImage(image, dstRectangle, srcRectangle, GraphicsUnit.Pixel);

            int _width = MonitorWidth;
            if (_width > 256 / 2)
                _width = 256 / 2;

            int _height = MonitorHeight;
            if (_height > 192 / 2)
                _height = 192 / 2;

            dstRectangle = new Rectangle(
                -ViewportX / 2, -ViewportY / 2, _width, _height
                );

            graphics.DrawRectangle(new Pen(Color.Red), dstRectangle);

            e.Graphics.DrawImage(bufferedImage, 0, 0);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
    }
}
